//
//  XJRetweetView.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/21.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class XJRetweetView: UIView {
    
    var bottomCons: Constraint?
    /// 设置转发微博的内容
    var homeCellViewModel: XJHomeCellViewModel?{
    
        didSet{
//            contentLabel.text = homeCellViewModel?.retweetText
            contentLabel.attributedText = homeCellViewModel?.reweetAttributedStr
            //判断转发微博是否有配图,更新转发微博的底部约束
            bottomCons?.uninstall()

            if let pic_urls = homeCellViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0{
                
                pictureView.dataInfo = (pic_urls, homeCellViewModel!.retweetPictureViewSize)
               
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                })
            
            }else{
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
        backgroundColor = UIColor(white: 252/255, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)

        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(cellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
        }
        self.snp_makeConstraints { (make) -> Void in
            
          bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
        }    
    }
    
    
    private lazy var contentLabel: YYLabel = {
    
//        UILabel(textColor: UIColor.darkGrayColor(), fontSize: contentLabelFont, preferredMaxLayoutWidth: SCREENW - 2 * cellMargin)
        
        let contentLabel = YYLabel()
        contentLabel.textColor = UIColor.darkGrayColor()
        contentLabel.font = UIFont.systemFontOfSize(contentLabelFont)
        contentLabel.preferredMaxLayoutWidth = SCREENW - 2 * cellMargin
        contentLabel.numberOfLines = 0
        contentLabel.userInteractionEnabled = true
        
        return contentLabel
    
    }()
    
    
    private lazy var pictureView: XJStatusPictureView = XJStatusPictureView(frame: CGRectZero,collectionViewLayout: UICollectionViewFlowLayout())

}
